﻿
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("OpenCV.NET Example");

        string Command;
        while (true)
        {
            Command = Console.ReadLine();
            if (Command == "exit")
            {
                break;
            }
            if (Command == "help")
            {
                Console.WriteLine("Available commands: exit, help");
                continue;
            }

            if ("vedio" == Command)
            {
                // 打开默认摄像头（通常为0，如果有多个摄像头，可以尝试1, 2等）
                using var capture = new VideoCapture(0);

                if (!capture.IsOpened())
                {
                    Console.WriteLine("无法打开摄像头");
                    return;
                }

                // 创建一个窗口来显示摄像头画面
                using var window = new Window("摄像头视频流");

                // 用于存储帧的 Mat 对象
                using var frame = new Mat();

                Console.WriteLine("按 'ESC' 键退出");

                while (true)
                {
                    // 读取一帧
                    capture.Read(frame);
                    // 检查帧是否为空
                    if (frame.Empty())
                    {
                        Console.WriteLine("无法读取帧");
                        break;
                    }
                    // 显示帧
                    window.ShowImage(frame);

                    // 检查用户是否按下 'ESC' 键
                    if (Cv2.WaitKey(1) == 27) // 27 是 'ESC' 键的 ASCII 码
                    {
                        break;
                    }
                }

                // 释放资源
                capture.Release();
                window.Dispose();
                continue;
            }

            if ("frame" == Command)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();

                using var capture = new VideoCapture(0);

                if (!capture.IsOpened())
                {
                    Console.WriteLine("无法打开摄像头");
                    return;
                }
                // 创建一个窗口来显示摄像头画面
                using var window = new Window("摄像头画面");

                // 读取一帧
                using var frame = new Mat();

                capture.Read(frame);
                // 显示摄像头画面
                window.ShowImage(frame);
                // 保存图片
                string imagePath = "captured_image.jpg";
                Cv2.ImWrite(imagePath, frame);

                stopwatch.Stop();
                Console.WriteLine($"执行耗时: {stopwatch.ElapsedMilliseconds} 毫秒");
                continue;
            }

            if ("capture" == Command)
            {
                // 打开默认摄像头（通常为0，如果有多个摄像头，可以尝试1, 2等）
                using var capture = new VideoCapture(0);

                if (!capture.IsOpened())
                {
                    Console.WriteLine("无法打开摄像头");
                    return;
                }

                // 创建一个窗口来显示摄像头画面
                using var window = new Window("摄像头画面");

                // 读取一帧
                using var frame = new Mat();

                capture.Read(frame);
                // 显示摄像头画面
                window.ShowImage(frame);

                // 等待用户按键
                Console.WriteLine("按任意键拍照并保存...");
                Console.ReadKey();

                // 再次读取一帧
                capture.Read(frame);

                // 保存图片
                string imagePath = "captured_image.jpg";
                Cv2.ImWrite(imagePath, frame);
                Console.WriteLine($"图片已保存为 {imagePath}");
                continue;
            }

            if (Command == "pic")
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                // Load an image from file
                using Mat image = Cv2.ImRead(@"../pic/horse.png");
                // Check if the image is loaded successfully
                if (image.Empty())
                {
                    Console.WriteLine("Could not load image.");
                    return;
                }
                // Display the image in a window
                Cv2.ImShow("Image", image);
                //Cv2.WaitKey(0); // Wait for a key press
                // Release resources
                Cv2.DestroyAllWindows();
                stopwatch.Stop();
                Console.WriteLine($"执行耗时: {stopwatch.ElapsedMilliseconds} 毫秒");
                continue;
            }

        }

    }//static void Main(string[] args)
}
